// pages/activitys/detail/detail.js
var WxParse = require('../../../wxParse/wxParse.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadModal: false,
    activityId: 0,
    info: {},
    users: [],
    isFavFlag: false,
    isJoinFlag: true,
    joinBtnText: '活动报名已截止',
    isPay: false,
    payBtnText: '',
    isCost: 0,
    cost: 0
  },

  /**
   * 转向报名页面
   * @param {*} e 
   */
  join(e) {
    if(this.data.isWxLogin){
      if(!this.data.isJoinFlag){
        wx.navigateTo({
          url: '../join/join?id=' + e.currentTarget.dataset.id + '&maxJoin=' + this.data.info.maximumPeople + 
          '&isCost=' + this.data.info.isCost + '&cost=' + this.data.info.cost
        });
      }
      
    }else{
      wx.redirectTo({
        url: '/pages/index/index?tab=my'
      });
    }
    
  },

  /**
   * 收藏活动
   */
  fav(e) {
    let _that = this;
    if(_that.data.isWxLogin){
      let _url = app.globalData.apiServerUrl + '/fav';
      let _isFavFlag = true;
      if(_that.data.isFavFlag){
        _url += '/remove';
        _isFavFlag = false;
      }
      wx.request({
        url: _url,
        method: 'POST',
        data: {
          userId: _that.data.userInfo.userId,
          activityId: this.data.activityId
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
        },
        success(res) {
          var _d = res.data;
          if(_d.status && _d.code == 200) {
            _that.setData({
              isFavFlag: _isFavFlag
            });
          }
        }
      });
    }else{
      wx.redirectTo({
        url: '/pages/index/index?tab=my'
      });
    }







  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      activityId: options.id,
      loadModal: true
    });
    this.getActivityInfo();
    this.getCooikeUser();
    
  },

  /**
   * 获取活动详细信息
   */
  getActivityInfo() {
    let _that = this;
    let _url = app.globalData.apiServerUrl + '/detail/' + this.data.activityId;
    wx.request({
      url: _url,
      success(res) {
        var _d = res.data;
        if(_d.code == 200 && _d.status){
          WxParse.wxParse('article' , 'html', _d.data.content, _that, 5);
          _that.setData({
            info: _d.data
          });
          _that.getJoinUserList();
        }else{
          _that.setData({
            loadModal: false
          });
        }
      }
    });

  },


  getCooikeUser() {
    let _isLogin = wx.getStorageSync('isWxLogin');
    if(!_isLogin){//TOOD 提示用户登录
      // wx.redirectTo({
      //   url: '/pages/index/index'
      // });
      this.setData({
        isWxLogin: false
      });
    }else{
      let _loginU = wx.getStorageSync('loginUserInfo');
      this.setData({
        userInfo: _loginU,
        isWxLogin: true
      });
    }
  },

  /**
   * 判断是否收藏当前活动
   */
  isFav() {
    let _that = this;
    if(_that.data.isWxLogin){

      let _url = app.globalData.apiServerUrl + '/fav/check';
      wx.request({
        url: _url,
        method: 'POST',
        data: {
          userId: _that.data.userInfo.userId,
          activityId: this.data.activityId
        },
        header: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
        },
        success(res) {
          var _d = res.data;
          if(_d.data) {
            _that.setData({
              isFavFlag: _d.data
            });
          }
        }
      });
    }
    
  },

  // 是否允许报名
  isJoin() {
    let _that = this;
    if(_that.data.info && _that.data.info.isRegister != '1'){ // 活动已停止
      _that.setData({
        joinBtnText: '活动报名已截止',
        isJoinFlag: true,
        isPay: false,
        loadModal: false
      });
    } else {
      
      if(_that.data.isWxLogin){
        let _url = app.globalData.apiServerUrl + '/join/check';
        wx.request({
          url: _url,
          method: 'POST',
          data: { userId: _that.data.userInfo.userId, activityId: _that.data.activityId },
          header: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'},
          success(res) {
            var _d = res.data;
            if(_d.data) {
              let _isJoinFlag = false, _joinBtnText = '我要报名', _isPay = false;
              if(_d.data.isJoin == 1){// 已加入
                _isJoinFlag = true;
                if(_d.data.pay == 1){ // 已支付
                  _joinBtnText = '报名成功';
                }else{
                  _joinBtnText = '我要报名';
                  _isJoinFlag = false;
                  //_isPay = false;
                  _isPay = false;
                }
              }else if(_that.data.users && _that.data.info.maximumPeople <= _that.data.users.length){
                _joinBtnText = '活动已满员';
                _isJoinFlag = true;
              }

              _that.setData({
                joinBtnText: _joinBtnText,
                isJoinFlag: _isJoinFlag,
                isPay: _isPay,
                loadModal: false
              });
              
              
  
            }
          }
        });
      }else{
        _that.setData({
          joinBtnText: '我要报名',
          loadModal: false
        });
      }

    }

    
  },

  // 获取当前活动的参加人员列表
  getJoinUserList(){
      let _that = this;
      let _url = app.globalData.apiServerUrl + '/join/user/' + this.data.activityId;
      wx.request({
        url: _url,
        success(res) {
          var _d = res.data;
          if(_d.code == 200 && _d.status){
            // if(_that.data.info.maximumPeople >= _d.data.length){
            //   _that.setData({
            //     joinBtnText: '活动已满员',
	          //     isJoinFlag: true,
            //   });
            // }
            _that.setData({
              users: _d.data
            });
          }

          _that.isFav();
          _that.isJoin();
        }
      });

  },







  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var _share = {
      title: this.data.info.title,
      imageUrl: this.data.info.coverImg,
      path: '/pages/activitys/detail/detail?id=' + this.data.activityId
    };
    return _share;
  }


})